//
//  BKGDPromotionsViewController.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/2/7.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKGDPromotionsViewController.h"
#import "BKGDCommon.h"
#import "BKGDObject.h"
#import "BKGDCell.h"

@interface BKGDPromotionsHeaderView : UICollectionReusableView

@end

@implementation BKGDPromotionsHeaderView {
    CALayer *_lineLayer;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    
    return self;
}

-(void)initialize {
    _lineLayer = [[CALayer alloc] init];
    _lineLayer.backgroundColor = HexColor(0xe0e0e0).CGColor;
    [self.layer addSublayer:_lineLayer];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    if(self.frame.size.height > 1) {
        _lineLayer.frame = CGRectMake(0, CGRectGetHeight(self.frame) / 2 - 0.5, CGRectGetWidth(self.frame), 1 / [[UIScreen mainScreen] scale]);
    } else {
        _lineLayer.frame = CGRectZero;
    }
}

@end


NSString *const BKGDPromotionsViewControllerIdentifier = @"BKGDPromotionsViewControllerIdentifier";
NSString *const BKGDPromotionsViewControllerHeaderIdentifier = @"BKGDPromotionsViewControllerHeaderIdentifier";

@interface BKGDPromotionsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation BKGDPromotionsViewController {
    UICollectionView *_collectionView;
    NSArray<NSString*> *_items;
    NSArray<BKGDKeyValueObject*> *_promotions;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeComponent];
}

-(void)initializeComponent {
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"BKGDSpecificationClose"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonTouch:)];
    [rightBarButton setImageInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[BKGDActivityCollectionViewCell class] forCellWithReuseIdentifier:BKGDPromotionsViewControllerIdentifier];
    [_collectionView registerClass:[BKGDPromotionsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:BKGDPromotionsViewControllerHeaderIdentifier];
    [self.view addSubview:_collectionView];
    
    [self makeConstraints];
}

-(void)makeConstraints {
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 15, 0, 0));
    }];
}

-(void)rightBarButtonTouch:(UIBarButtonItem*)sender {
    if(_closeBlock) {
        _closeBlock();
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1 + self.data.promotions.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(section == 0) {
        return _items.count;
    } else {
        return 1;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    CGSize size;
    if(section == 0){
        size = CGSizeZero;
    } else {
//        if(_items.count > 0) {
//            size = CGSizeMake(CGRectGetWidth(collectionView.frame), 10);
//        } else {
//            size = CGSizeZero;
//        }
        if(_items.count == 0 && section == 1) {
            size = CGSizeZero;
        } else {
            size = CGSizeMake(CGRectGetWidth(collectionView.frame), 10);
        }
    }
    
    return size;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size;
    
    if(indexPath.section == 0) {
        
        NSString *text = _items[indexPath.row];
        
        size = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]}];
        size.width += 4;
        size.height = 16;
        
    } else {
        
        size = CGSizeMake(CGRectGetWidth(collectionView.frame), 16);
    }
    
    return size;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *view;
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:BKGDPromotionsViewControllerHeaderIdentifier forIndexPath:indexPath];
    }
    
    return view;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BKGDActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BKGDPromotionsViewControllerIdentifier forIndexPath:indexPath];
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        [self configCell:cell atIndexPath:indexPath];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    [self configCell:(BKGDActivityCollectionViewCell*)cell atIndexPath:indexPath];
}

-(void)configCell:(BKGDActivityCollectionViewCell*)cell atIndexPath:(NSIndexPath*)indexPath {
    
    if(indexPath.section == 0) {
        cell.titleLabel.text = _items[indexPath.row];
        cell.subTitleLabel.text = @"";
    } else{
        cell.titleLabel.text = _promotions[indexPath.section - 1].value;
        cell.subTitleLabel.text = _promotions[indexPath.section - 1].key;
    }
}

-(void)setData:(BKGDObject *)data {
    _data = data;
    if(!data || ![data isKindOfClass:[BKGDObject class]]) {
        return;
    }
    
    [_collectionView.collectionViewLayout invalidateLayout];
    
    BKGDObject *model = data;
    
    _items = model.activities;
    _promotions = model.promotions;
    
    [_collectionView reloadData];
}

@end
